<?php
namespace Admin\Controller;
use Think\Controller;
class RegController extends Controller {
    
    public function index()
    {	
    	$this->assign();
    	$this->display('admin-add');
    }

    //检测申请注册的数据 没问题就新增到数据库
    public function checkdata()
    {
    	//var_dump($_POST);
    	// 'username' => string '1' (length=1)
		  // 'realname' => string '' (length=0)
		  // 'password' => string '123456' (length=6)
		  // 'repass' => string '123456' (length=6)
		  // 'qq' => string '' (length=0)
		  // 'phone' => string '18922476635' (length=11)
		  // 'section' => string '' (length=0)
		  // 'part' => string ''
		$regname = I('post.username');
		$res = M('user')->where('username='.$regname)->find();
		if ($res) {
			$arr = array(
    				'error'=>1,
    				'info'=>'登陆名已存在,请重新输入',
    			);
    		echo json_encode($arr);
    		exit;
		}

		if (empty(I('post.realname'))) {
    		$arr = array(
    				'error'=>1,
    				'info'=>'真实姓名为必填项',
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	if (empty(I('post.section'))) {
    		$arr = array(
    				'error'=>1,
    				'info'=>'部门为必填项',
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	if (empty(I('post.part'))) {
    		$arr = array(
    				'error'=>1,
    				'info'=>'角色为必填项',
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	//没问题时执行注册
    	//INSERT INTO stu (name,qq) VALUES ('学并思','113664000'); 
    	$data['username'] = I('post.username');
    	$data['realname'] = I('post.realname');
    	$data['password'] = md5(I('post.password'));
    	$data['qq'] = I('post.qq');
    	$data['phone'] = I('post.phone');
    	$data['section'] = I('post.section');
    	$data['part'] = I('post.part');
    	$data['create_time'] = time();
    	$User = M("User"); // 实例化User对象
		$res = $User->add($data);
		if ($res) {
			$arr = array(
				'error'=>0,
				'info'=>'增加成功!',
			);
			echo json_encode($arr);
			exit;
		}else{
			$arr = array(
				'error'=>1,
				'info'=>'增加中出现错误= =!',
			);
			echo json_encode($arr);
			exit;
		}

    }
}